//using PK.InAppPurchase;
using PKGames.Localization;
//using PKGames.Store;
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using Utils;

namespace PKGames
{
    public class IapCreativeUiController : MonoBehaviour
    {
        public enum IapTrigger
        {
            LevelTrial,
            LevelTimerEnded,
            LevelLocked,
            Unknown
        }

        [SerializeField]
        private Text _titleText;

        [SerializeField]
        private Text _contentText;

        [SerializeField]
        private Button _buyNowButton;

        [SerializeField]
        private Text _buyNowButtonText;

        [SerializeField]
        private GameObject _closeButtonObject;

        [SerializeField]
        private Button _closeButton;

        [SerializeField]
        private Button _closeButtonBg;

        [SerializeField]
        private Image _levelThumbImage;

        [SerializeField]
        private CanvasGroup _iapCreativeCanvasGroup;

        private const string LockedTitleKey = "IapTrialOverKey";

        private const string LockedContentKey = "IapUnlockKey";

        private const string TrialTitleKey = "IapTrialKey";

        private const string TrialContentKey = "IapUnlockKey";

        private const string ButtonPriceKey = "IapBuyNowKey";

        //private StoreProduct _storeProduct;

        private IapTrigger _trigger = IapTrigger.Unknown;

        private string _levelName;

        private const string CreativeName = "base";

        public void Setup(Sprite sprite, object storeProduct, string levelName)
        {
            _levelName = levelName;
            _levelThumbImage.sprite = sprite;
            //_storeProduct = storeProduct;
        }

        public IEnumerator ShowIapCreativePopup(IapTrigger iapTrigger, float fadeTime, UnityAction onClose)
        {
            _trigger = iapTrigger;
            SetupSkin(iapTrigger);
            _iapCreativeCanvasGroup.interactable = false;
            _iapCreativeCanvasGroup.blocksRaycasts = false;
            yield return _iapCreativeCanvasGroup.Fade(0f, 1f, fadeTime);
            _iapCreativeCanvasGroup.interactable = true;
            _iapCreativeCanvasGroup.blocksRaycasts = true;
            _closeButton.onClick.RemoveAllListeners();
            _closeButton.onClick.AddListener(onClose);
            _buyNowButton.onClick.RemoveAllListeners();
            _buyNowButton.onClick.AddListener(BuyProduct);
            if (iapTrigger != IapTrigger.LevelTimerEnded)
            {
                _closeButtonBg.onClick.RemoveAllListeners();
                _closeButtonBg.onClick.AddListener(onClose);
            }
            //StoreEvents.LogCreativeOpen(_storeProduct, _trigger.ToString().ToLowerInvariant(), "base", _levelName);
        }

        private void SetupSkin(IapTrigger iapTrigger)
        {
            switch (iapTrigger)
            {
                case IapTrigger.LevelTrial:
                    SetupTrialCreative();
                    break;
                case IapTrigger.LevelTimerEnded:
                    SetupForcedCreative();
                    break;
                case IapTrigger.LevelLocked:
                    SetupLockedCreative();
                    break;
                default:
                    throw new ArgumentOutOfRangeException("iapTrigger", iapTrigger, null);
            }
        }

        private void SetupTrialCreative()
        {
            _titleText.text = MonoSingleton<LocationProvider>.Instance.GetLocalizedString("IapTrialKey");
            _contentText.text = MonoSingleton<LocationProvider>.Instance.GetLocalizedString("IapUnlockKey");
            //_buyNowButtonText.text = GenerateButtonText();
            _closeButtonObject.SetActive(value: false);
        }

        private void SetupForcedCreative()
        {
            _titleText.text = MonoSingleton<LocationProvider>.Instance.GetLocalizedString("IapTrialOverKey");
            _contentText.text = MonoSingleton<LocationProvider>.Instance.GetLocalizedString("IapUnlockKey");
            //_buyNowButtonText.text = GenerateButtonText();
            _closeButtonObject.SetActive(value: false);
        }

        private void SetupLockedCreative()
        {
            _titleText.text = MonoSingleton<LocationProvider>.Instance.GetLocalizedString("IapTrialOverKey");
            _contentText.text = MonoSingleton<LocationProvider>.Instance.GetLocalizedString("IapUnlockKey");
            //_buyNowButtonText.text = GenerateButtonText();
            _closeButtonObject.SetActive(value: true);
        }

        //private string GenerateButtonText()
        //{
            //string localizedString = MonoSingleton<LocationProvider>.Instance.GetLocalizedString("IapBuyNowKey");
            //localizedString = localizedString.Replace("{0}", StoreManager.Instance.GetCurrencyForProduct(_storeProduct));
            //return localizedString.Replace("{1}", StoreManager.Instance.GetPriceForProduct(_storeProduct));
        //}

        public IEnumerator Close(float fadeTime)
        {
            _iapCreativeCanvasGroup.interactable = false;
            _iapCreativeCanvasGroup.blocksRaycasts = false;
            yield return _iapCreativeCanvasGroup.Fade(_iapCreativeCanvasGroup.alpha, 0f, fadeTime);
        }

        private void BuyProduct()
        {
            //StoreManager.Instance.PurchaseStoreProduct(_storeProduct, _trigger.ToString().ToLowerInvariant(), "base", _levelName);
        }
    }
}
